package com.joker.orange.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.joker.common.core.page.TableDataInfo;
import com.joker.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.joker.common.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.joker.orange.domain.bo.CaricatureCategoryBo;
import com.joker.orange.domain.vo.CaricatureCategoryVo;
import com.joker.orange.domain.CaricatureCategory;
import com.joker.orange.mapper.CaricatureCategoryMapper;
import com.joker.orange.service.ICaricatureCategoryService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 漫画分类Service业务层处理
 *
 * @author Joker
 * @date 2024-06-09
 */
@RequiredArgsConstructor
@Service
public class CaricatureCategoryServiceImpl implements ICaricatureCategoryService {

    private final CaricatureCategoryMapper baseMapper;

    /**
     * 查询漫画分类
     */
    @Override
    public CaricatureCategoryVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    @Override
    public CaricatureCategory queryByCategoryName(String categoryName) {
        return baseMapper.queryByCategoryName(categoryName);
    }

    /**
     * 查询漫画分类列表
     */
    @Override
    public TableDataInfo<CaricatureCategoryVo> queryPageList(CaricatureCategoryBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<CaricatureCategory> lqw = buildQueryWrapper(bo);
        Page<CaricatureCategoryVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询漫画分类列表
     */
    @Override
    public List<CaricatureCategoryVo> queryList(CaricatureCategoryBo bo) {
        LambdaQueryWrapper<CaricatureCategory> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<CaricatureCategory> buildQueryWrapper(CaricatureCategoryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<CaricatureCategory> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getCategoryName()), CaricatureCategory::getCategoryName, bo.getCategoryName());
        lqw.eq(bo.getParentId() != null, CaricatureCategory::getParentId, bo.getParentId());
        lqw.eq(bo.getOrderNum() != null, CaricatureCategory::getOrderNum, bo.getOrderNum());
        lqw.eq(StringUtils.isNotBlank(bo.getVisible()), CaricatureCategory::getVisible, bo.getVisible());
        lqw.eq(StringUtils.isNotBlank(bo.getIcon()), CaricatureCategory::getIcon, bo.getIcon());
        return lqw;
    }

    /**
     * 新增漫画分类
     */
    @Override
    public Boolean insertByBo(CaricatureCategoryBo bo) {
        CaricatureCategory add = BeanUtil.toBean(bo, CaricatureCategory.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改漫画分类
     */
    @Override
    public Boolean updateByBo(CaricatureCategoryBo bo) {
        CaricatureCategory update = BeanUtil.toBean(bo, CaricatureCategory.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(CaricatureCategory entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除漫画分类
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
